#include "Program.h"
#include "OpenGLRenderer.h"

using namespace milo;

Program::Program(void)
{
	_id = glCreateProgram();
}

Program::~Program(void)
{
	if ( glIsProgram( _id ) )
		glDeleteProgram( _id );
}


bool Program::createProgram(const std::vector<GLuint> &shaders)
{
	for(size_t iLoop = 0; iLoop < shaders.size(); iLoop++)
	{
		glAttachShader( _id, shaders[iLoop]);
	}

	glLinkProgram(_id);
	
	GLint status;
	glGetProgramiv (_id, GL_LINK_STATUS, &status);
	if (status == GL_FALSE) 
	{
		GLint infoLogLength;
		glGetProgramiv(_id, GL_INFO_LOG_LENGTH, &infoLogLength);

		GLchar *strInfoLog = new GLchar[infoLogLength + 1];
		glGetProgramInfoLog(_id, infoLogLength, NULL, strInfoLog);
		fprintf(stderr, "Linker failure: %s\n", strInfoLog);
		delete[] strInfoLog;
	}

	for(size_t iLoop = 0; iLoop < shaders.size(); iLoop++)
	{
		glDetachShader( _id, shaders[iLoop] );	
	}

	return true;
}

GLuint Program::getId() const
{
	return _id;
}